package com.me.modules.sys.controller;

import com.me.modules.common.annotation.SysLog;
import com.me.modules.common.common.Constant;
import com.me.modules.common.controller.BaseController;
import com.me.modules.common.utils.JsonUtil;
import com.me.modules.common.utils.PageUtils;
import com.me.modules.common.utils.Query;
import com.me.modules.common.utils.Result;
import com.me.modules.sys.entity.GroupEntity;
import com.me.modules.sys.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 工作组
 *
 * @author zhenglei
 * @Date 2018-12-18 10:07:59
 */
@RestController
@RequestMapping("sys/group")
public class GroupController extends BaseController {
//	@Autowired
//	private RoleService roleService;
//	@Autowired
//    private RoleMenuService roleMenuService;
	@Autowired
	private GroupService groupService;
	@Autowired
	private GroupRoleService groupRoleService;
	@Autowired
	private GroupUserService groupUserService;
	@Autowired
	private OperationGroupService operationGroupService;


	/**
	 * 列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:group:list")
	@SysLog("查看组列表")
	public Result list(@RequestParam Map<String, Object> params){
		//查询列表数据
        Query query = new Query(params);

		List<GroupEntity> groupList = groupService.queryList(query);
		int total = groupService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(groupList, total, query.getLimit(), query.getPage());

		return Result.ok().put("page", pageUtil);
	}


	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	@RequiresPermissions("sys:group:info")
	@SysLog("查看组信息")
	public Result info(@PathVariable("id") Integer id){
		GroupEntity group = groupService.queryObject(id);
		group.setOperationIdList(operationGroupService.queryOperationIdListByGroupId(id));
		group.setUserIdList(groupUserService.queryUserIdListByGroupId(id));
		group.setRoleIdList(groupRoleService.queryRoleIdListByGroupId(id));
        return Result.ok().put("group", group);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	@RequiresPermissions("sys:group:update")
	@SysLog("新增组")
	public Result save(@RequestBody GroupEntity group){
        Result result = Result.ok();
		try {
			List<GroupEntity> groupEntities = groupService.queryListByGroupName(group.getName());
			if(groupEntities != null && groupEntities.size() > 0){
				result=Result.error("组名称不能重复!!!");
			}else{
				groupService.save(group);
			}
		} catch (Exception e) {
			result=Result.error();
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 组列表
	 */
	@RequestMapping("/select")
	public Result select(){
		Map<String, Object> map = new HashMap<String, Object>();

		List<GroupEntity> list = groupService.queryList(map);

		return Result.ok().put("list", list);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	@RequiresPermissions("sys:role:update")
	@SysLog("修改组")
	public Result update(@RequestBody GroupEntity group){
		Result result = Result.ok();
		try {
			Map<String,Object> map = new HashMap<>();
			map.put("name",group.getName());
			map.put("id",group.getId());
			List<GroupEntity> groupEntities = groupService.checkListByGroupName(map);
			if(groupEntities != null && groupEntities.size() > 0){
				result=Result.error("组名称不能重复!!!");
			}else{
				groupService.update(group);
			}
		} catch (Exception e) {
			e.printStackTrace();
			result=Result.error("修改组失败!!!");
		}
		return result;
	}

	/**
	 * 禁用
	 */
	@RequestMapping("/delete")
	@RequiresPermissions("sys:group:delete")
	@SysLog("禁用组")
	public Result delete(@RequestBody String[] ids){
        Result result = Result.ok();
        try {
			groupService.updateBatchStatus(ids,Constant.DELETE_STATUS.NO.getValue());
        } catch (Exception e) {
            result=result.error();
            e.printStackTrace();
        }
        return result;
	}

	/**
	 * 启用
	 */
	@RequestMapping("/enabled")
	@RequiresPermissions("sys:group:enabled")
	@SysLog("启用组")
	public Result enabled(@RequestBody String[] ids){
		Result result = Result.ok();
		try {
			groupService.updateBatchStatus(ids,Constant.DELETE_STATUS.YES.getValue());
		} catch (Exception e) {
			result=result.error();
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 增加组内用户
	 */
	@RequestMapping("/addGroupUser")
	@SysLog("增加组内用户")
	public Result addGroupUser(@RequestBody String params){
		Result result = Result.ok();
		try {
			Map<String,Object> map = new HashMap<>();
			map = JsonUtil.getObjet(params,Map.class);
//			map.put("groupId",params.get("groupId"));
			map.put("userIdList",(List)map.get("userIds"));
			groupUserService.groupAddUser(map);
		} catch (Exception e) {
			result=result.error();
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 删除组内用户
	 */
	@RequestMapping("/deleteGroupUser")
	@SysLog("删除组内用户")
	public Result deleteGroupUser(@RequestBody String params){
		Result result = Result.ok();
		try {
			Map<String,Object> map = new HashMap<>();
			map = JsonUtil.getObjet(params,Map.class);
//			map.put("groupId",params.get("groupId"));
			map.put("userIdList",(List)map.get("userIds"));
			groupUserService.groupDeleteUser(map);
		} catch (Exception e) {
			result=result.error();
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 查询用户所属所有工作组
	 */
	@RequestMapping("/userGroup")
	@SysLog("查询用户所属组")
	public Result userGroup(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<GroupEntity> groupUser = groupService.queryGroupListByUserId(query);
		int total = groupService.queryGroupTotalByUserId(query);
		PageUtils pageUtil = new PageUtils(groupUser, total, query.getLimit(), query.getPage());
		return Result.ok().put("page", pageUtil);
	}
}
